﻿using UnityEngine;
using System.Collections;

public class LifeCountDown : MonoBehaviour {

	public float LifeTime = 5f;
	float LifeTimeStart = 5f;
	public bool LifeActivated = false;
	public float FadeTime = 1f;
	float StartFade = 1f;
	bool deactivatedCollider = false;
	bool isEgg = false;
	bool eggRotationCalled = false;
	bool HasCollided = false;

	// This is dirty I know, but gets the job done. :(
	bool tick5 = false;
	bool tick4 = false;
	bool tick3 = false;
	bool tick2 = false;
	bool tick1 = false;
	bool tick0 = false;

	TextMesh eggTimerText;

	// Use this for initialization
	void Start () {
		this.LifeTimeStart = this.LifeTime;
		this.StartFade = this.FadeTime;
		if (this.gameObject.tag == "Egg") {
			this.isEgg = true;
			this.eggTimerText = (TextMesh)this.gameObject.transform.FindChild("EggText").gameObject.GetComponent(typeof(TextMesh));
		}
	}
	
	// Update is called once per frame
	void Update () {
		if (this.LifeActivated) {
			LifeTime -= Time.deltaTime;
			// Dirty I know, but gets the job done...

			/*
			if (LifeTime <= 3f && !tick3) {
				Debug.Log("PLAY 3");
				tick3 = true;
				GameMaintainerSingleton.Instance.PlayBombTickSound();
			}
			*/
			if (this.isEgg) {
				if (LifeTime <= 5f && !tick5 && !HasCollided) {
					tick5 = true;
					this.eggTimerText.text = "5";
					//GameMaintainerSingleton.Instance.PlayBombTickSound();
				}
				if (LifeTime <= 4f && !tick4 && !HasCollided) {
					tick4 = true;
					this.eggTimerText.text = "4";
					//GameMaintainerSingleton.Instance.PlayBombTickSound();
				}
				if (LifeTime <= 3f && !tick3 && !HasCollided) {
					tick3 = true;
					this.eggTimerText.text = "3";
					//GameMaintainerSingleton.Instance.PlayBombTickSound();
				}
				if (LifeTime <= 2f && !tick2 && !HasCollided) {
					tick2 = true;
					this.eggTimerText.text = "2";
					GameMaintainerSingleton.Instance.PlayBombTickSound();
				}
				if (LifeTime <= 1f && !tick1 && !HasCollided) {
					tick1 = true;
					this.eggTimerText.text = "1";
					GameMaintainerSingleton.Instance.PlayBombTickSound();
				}
			}

			if (LifeTime < 0) {
				if (!deactivatedCollider) {
					CircleCollider2D cir = (CircleCollider2D)this.gameObject.GetComponent(typeof(CircleCollider2D));
					if (cir != null) {
						cir.enabled = false;
					}
					deactivatedCollider = true;
				}
				FadeTime -= Time.deltaTime;
				if (this.isEgg) {
					if (!tick0) {
						this.eggTimerText.text = "0";
						tick0 = true;
					}
					if (!this.eggRotationCalled) {
						//Debug.Log("EGG ROTATION");
						this.gameObject.transform.rotation = Quaternion.identity;

						this.gameObject.rigidbody2D.fixedAngle = true;
						this.gameObject.rigidbody2D.isKinematic = true;
						this.gameObject.rigidbody2D.gravityScale = 0f;
						this.gameObject.rigidbody2D.velocity = Vector2.zero;
						this.gameObject.rigidbody2D.Sleep();
						this.eggRotationCalled = true;
						GameMaintainerSingleton.Instance.ScreenShake(this.FadeTime);
					}
					this.gameObject.transform.localScale *= 1.05f;
				}
				else {
					Color col = this.gameObject.renderer.material.color;
					//col.a = Mathf.Lerp(100f,0f,FadeTime / MaxFadeTime * 100f / 100f);
					col.a = Mathf.Lerp(0f, 1f, FadeTime * (1f / StartFade));
					this.gameObject.renderer.material.color = col;
				}
				if (FadeTime < 0f) {
					if (this.isEgg) {
						if (!HasCollided) {
							GameMaintainerSingleton.Instance.ItemTimePassed();
						}
					}
					GameObject.Destroy(this.gameObject);
				}
			}
		}
	}

	void OnCollisionEnter2D(Collision2D other) {
		if (other.collider.tag == "Player") {
			//Debug.Log("Got to collision");
			//CircleCollider2D col = (CircleCollider2D)this.gameObject.GetComponent(typeof(CircleCollider2D));
			//col.enabled = false;
			//LifeCountDown cDown = (LifeCountDown)this.gameObject.GetComponent(typeof(LifeCountDown));
			//GameMaintainerSingleton.Instance.TouchedItem(cDown.LifeTime);
			this.HasCollided = true;
		}
	}

}
